package com.yudaob2b.elong.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.service.ElongHotelInventoryService;
import com.yudaob2b.business.service.ElongHotelListService;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.elong.constant.Constant;
import com.yudaob2b.elong.constant.RedisConstant;
import com.yudaob2b.elong.listener.aliyun.mq.MyMessageConsumer;
import com.yudaob2b.elong.task.IncrInvProducerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/8/10.
 * author kidd.Tan
 *
 * @author Administrator
 * @version $Id: $Id
 */
@Service("startInitService")
public class StartInitService implements InitializingBean, ServletContextAware {
    private static final Logger logger = LoggerFactory.getLogger(StartInitService.class);

    @Autowired
    private IncrInvProducerTask incrInvProducerTask;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ElongHotelListService elongHotelListService;

    @Autowired
    private ElongHotelInventoryService elongHotelInventoryService;

    /**
     * {@inheritDoc}
     */
    @Override
    public void afterPropertiesSet() {
        logger.info("begin........................");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        logger.info("start init base date........................");
        // refreshData
        //refreshData();

        // 系统启动的时候启动MQ订阅
        //new MyMessageConsumer().subscribe();

        // 启动国内酒店-库存增量线程
        //runIncrInv();


    }

    private void refreshData() {
    }

    /**
     * 启动国内酒店-库存增量线程
     */
    private void runIncrInv() {
        // 重置库存增量同步时间
        Date updateTime;
        Object updateTimeObj = redisTemplate.opsForValue().get(RedisConstant.REDIS_INCR_INV_UPDATE_TIME_KEY);
        if (updateTimeObj != null) {
            updateTime = (Date) updateTimeObj;
            if (DateUtil.rollDate(updateTime, Constant.INCR_HOUR_LIMIT, 0).compareTo(new Date()) < 0) {
                updateTime = DateUtil.rollDate(new Date(), 0, -Constant.INCR_AHEAD_MINUTE);
            } else {
                updateTime = DateUtil.rollDate((Date) updateTimeObj, 0, -Constant.INCR_AHEAD_MINUTE);
            }
        } else {
            updateTime = DateUtil.rollDate(new Date(), 0, -Constant.INCR_AHEAD_MINUTE);
        }
        redisTemplate.opsForValue().set(RedisConstant.REDIS_INCR_INV_UPDATE_TIME_KEY, updateTime);
        // 重启删掉最后更新id
        redisTemplate.delete(RedisConstant.REDIS_INCR_INV_LASTID_KEY);
        // 异步启动库存增量生产者线程
        incrInvProducerTask.incrInvProducer();
        /*try {
            Thread.sleep(3000);
            // 库存增量消费者开10个线程消费
            for (int i = 0; i < 200; i++) {
                incrInvProducerTask.incrInvCustomer();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/


    }

}
